﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace homework
{
    public class OrderDetails
    {
        public string ProductName { get; set; }
        public double Price { get; set; }
        public int Quantity { get; set; }

        //添加ToString方法用来显示订单信息
        public override string ToString()
        {
            return $"Product:{ProductName}, Price: {Price}, Quantity: {Quantity}";
        }
    }

    //订单类
    public class Order
    {
        public string OrderId { get; set; }
        public string Customer { get; set; }
        public List<OrderDetails> Details { get; set; } = new List<OrderDetails>();
        public double TotalAmount => Details.Sum(d => d.Price * d.Quantity);
        public override string ToString()
        {
            return $"Order ID: {OrderId}, Customer: {Customer}, Total Amount: {TotalAmount}";
        }
        //重写equals方法
        public override bool Equals(object obj)
        {
            if (!(obj is Order otherOrder))
                return false;
            return OrderId == otherOrder.OrderId;
        }
        public override int GetHashCode()
        {
            return OrderId.GetHashCode();
        }
    }
    //订单服务类
    public class OrderService
    {
        public List<Order> orders = new List<Order>();
        //添加订单
        public void AddOrder(Order order)
        {
            if (orders.Contains(order))
            {
                throw new ArgumentException("Order already exist");
            }
            orders.Add(order);
        }
        //删除订单
        public void RemoveOrder(string orderId)
        {
            //找到第一个与传入的对象id值相等的订单
            var order = orders.FirstOrDefault(o => o.OrderId == orderId);
            if (order == null)
            {
                throw new ArgumentException("order not found");

            }
            orders.Remove(order);
        }
        //修改订单
        public void ModifyOrder(string orderId, Order newOrder)
        {
            var index = orders.FindIndex(o => o.OrderId == orderId);
            if (index < 0)
            {
                throw new ArgumentException("Order not found!");
            }
            orders[index] = newOrder;
        }
        //查询订单
        public IEnumerable<Order> QueryOrders(Func<Order, bool> predicate)
        {
            return orders.Where(predicate).OrderBy(o => o.TotalAmount);
        }
        // 排序订单
        public void SortOrders()
        {
            orders.Sort((x, y) => x.OrderId.CompareTo(y.OrderId));//comparedto定义的是比较规则
                                                                  //Sort方法用来接收一个返回值为Comparsion类型的对象
        }

        //导出订单到XML文件
        public void Export(string filePath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                serializer.Serialize(writer, orders);
            }
        }

        //从XML文件中导出订单
        public void Import(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("xml file not found", filePath);
            }
            XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
            using (StreamReader reader=new StreamReader(filePath))
            {
                var importOrders =(List<Order>)serializer.Deserialize(reader);
                orders.AddRange(importOrders);
            }
        }
    }
}
